﻿using System;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
using System.Collections.Generic;
using System.Linq;

namespace UscSunShine.Models
{
    /// <summary>
    /// 请求信息
    /// </summary>
    public class Request
    {
        public Request()
        {
            this.RequestCategory = RequestCategory.咨询;
            this.ProcessStatus = ProcessStatus.审核中;
            this.ExpectResolveDate = DateTime.Now.AddDays(7);
            this.AcceptDate = DateTime.Now;
            this.ViewTimes = 1;
        }

        #region 属性
        /// <summary>
        /// 流水号
        /// </summary>        
        public int ID { get; set; }

        /// <summary>
        /// 受理单位
        /// </summary>      
        public Department Department { get; set; }

        /// <summary>
        /// 受理单位ID
        /// </summary>
        [Required(ErrorMessage = "请选择受理单位")]
        [Display(Name = "受理单位")]
        public int DepartmentID { get; set; }

        /// <summary>
        /// 请求类别
        /// </summary>
        [Required(ErrorMessage = "请选择类别")]
        [EnumDataType(typeof(RequestCategory))]
        [Display(Name = "申请类别")]
        public RequestCategory RequestCategory { get; set; }

        /// <summary>
        /// 处理状态
        /// </summary>
        [EnumDataType(typeof(ProcessStatus))]
        [Display(Name = "处理状态")]
        public ProcessStatus ProcessStatus { get; set; }

        /// <summary>
        /// 受理单号
        /// </summary>
        [Display(Name = "受理单号")]
        public string AcceptNumber { get; set; }

        /// <summary>
        /// 主题
        /// </summary>
        [Required(ErrorMessage = "请填写主题")]
        [Display(Name = "主题")]
        public string Title { get; set; }

        /// <summary>
        /// 请求内容
        /// </summary>
        [Required(ErrorMessage = "请填写正文")]
        [Display(Name = "正文")]
        [AllowHtml]
        public string Content { get; set; }

        /// <summary>
        /// 用户姓名
        /// </summary>
        [Required(ErrorMessage = "请填写用户姓名")]
        [Display(Name = "用户姓名")]
        public string Username { get; set; }

        private const string regStr = @"((1\d{10})|^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1}))$)";
        /// <summary>
        /// 联系电话
        /// </summary>
        [Required(ErrorMessage = "请填写联系电话")]
        [RegularExpression(regStr, ErrorMessage = "电话格式：11位手机号码 或 3-4位区号，7-8位号码 XXXX-XXXXXXX")]
        [Display(Name = "联系电话")]
        public string ContactPhone { get; set; }

        [Required(ErrorMessage = "请填写邮件地址")]
        [DataType(DataType.EmailAddress, ErrorMessage = "请填写正确的邮件地址")]
        [Display(Name = "电子邮件")]
        public string Email { get; set; }

        /// <summary>
        /// 期望解决日期
        /// </summary>
        [Display(Name = "期望解决日期")]
        [DataType(DataType.Date, ErrorMessage = "日期格式：2014-02-08")]
        //[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}")]
        public DateTime ExpectResolveDate { get; set; }

        /// <summary>
        /// 受理时间
        /// </summary>
        /// <remarks></remarks>
        [Display(Name = "提交时间")]
        [DataType(DataType.Date, ErrorMessage = "日期格式：2014-02-08")]
        //[DisplayFormat(DataFormatString="{0:yyyy-MM-dd}")]
        public DateTime AcceptDate { get; set; }

        /// <summary>
        /// 审核时间
        /// </summary>
        /// <remarks></remarks>
        [Display(Name = "审核时间")]
        [DataType(DataType.Date, ErrorMessage = "日期格式：2014-02-08")]
        //[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}")]
        public DateTime? AuditDate { get; set; }

        /// <summary>
        /// 查看次数
        /// </summary>
        /// <remarks>查看次数</remarks>
        [Display(Name = "查看次数")]
        [Range(1, 100000)]
        public int ViewTimes { get; set; }

        /// <summary>
        /// 回复内容
        /// </summary> 
        [Display(Name = "回复内容")]
        [AllowHtml]
        public string RespondContent { get; set; }

        /// <summary>
        /// 处理时间
        /// </summary>        
        [Display(Name = "处理时间")]
        [DataType(DataType.Date, ErrorMessage = "日期格式：2014-02-08")]
        public DateTime? ProcessDate { get; set; }
        #endregion

        #region 业务规则
        /// <summary>
        /// 生成受理单号
        /// </summary>
        public void CreateAcceptNumber()
        {
            AcceptNumber = string.Format("{0:yyyyMMddHHmmssffff}", this.AcceptDate);
        }

        /// <summary>
        /// 需要审核的处理状态
        /// </summary>
        /// <returns></returns>
        public static IEnumerable<ProcessStatus> StatusNeedAudit
        {
            get
            {
                var status = new ProcessStatus[] { ProcessStatus.审核中 };
                return status;
            }
        }

        /// <summary>
        /// 允许浏览的状态
        /// </summary>
        /// <returns></returns>
        public static IEnumerable<ProcessStatus> StatusCanBrowse
        {
            get
            {
                var status = new ProcessStatus[] 
            { 
                ProcessStatus.已受理, 
                ProcessStatus.正在办理, 
                ProcessStatus.按时办结, 
                ProcessStatus.超期办结, 
                ProcessStatus.超期未办结 
            };
                return status;
            }
        }

        /// <summary>
        /// 未办结的状态
        /// </summary>
        public static IEnumerable<ProcessStatus> StatusProcessing
        {
            get
            {
                var processing = new ProcessStatus[] 
                { 
                    UscSunShine.Models.ProcessStatus.正在办理, 
                    UscSunShine.Models.ProcessStatus.超期未办结,
                    UscSunShine.Models.ProcessStatus.已受理
                };

                return processing;
            }
        }

        /// <summary>
        /// 已办结的状态
        /// </summary>
        public static IEnumerable<ProcessStatus> StatusProcessed
        {
            get
            {
                var processed = new ProcessStatus[] 
                { 
                    ProcessStatus.按时办结, 
                    ProcessStatus.超期办结 
                };
                return processed;
            }
        }

        /// <summary>
        /// 审核
        /// </summary>
        /// <param name="isPassed">是否通过</param>
        public void Audit(bool isPassed)
        {
            this.AuditDate = DateTime.Now;
            if (isPassed)
            {
                this.ProcessStatus = ProcessStatus.正在办理;
            }
            else
            {
                this.ProcessStatus = ProcessStatus.拒绝受理;
            }
        }

        #endregion


        public void Process()
        {
            this.ProcessDate = DateTime.Now;
            //当期时间与期望解决日期比较
            var diffence = DateTime.Now.CompareTo(this.ExpectResolveDate);
            //未超期
            this.ProcessStatus = diffence <= 0 ? ProcessStatus.按时办结 : ProcessStatus.超期办结;
        }
    }
}
